﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Prod_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:UpdatePanel ID="UpdatePanelusr" runat="server" UpdateMode="Conditional">
        <ContentTemplate>            
            <h1 title="OU, Companykey, Groupmembership and address details copied into the form.">Load information from existing user (optional):</h1>    
            <p>
                <label for="ExistUsrid" class="label">Search User-ID:</label>
                <asp:TextBox runat="server" ID="txtExistUsrId" EnableViewState="False"></asp:TextBox>
                <span class="labelact">
                    <asp:LinkButton class="lbtnclLoadExistUsr" ID="lbtnLoadExistUsr" runat="server" OnClick="lbtnLoadExistUsr_Click" ClientIDMode="Static" EnableViewState="False" CausesValidation="False">&rsaquo;&rsaquo;&nbsp;Load existing user information.</asp:LinkButton>
                </span>                
            </p>                
            <div class="divExistUsr">                  
                <asp:Label ID="lblLoadExistUsrRslt" runat="server" ClientIDMode="Static"></asp:Label>
                <p class="small"></p>                    
                <asp:Label ID="lblExistUsrFullName" runat="server" ClientIDMode="Static"></asp:Label>
            </div> 
            <p></p>
            <h1>Important information for new user:<asp:HiddenField ID="htxtCompanyKey" runat="server" /> </h1>                
            <p></p>
            <p>                    
                <asp:Label CssClass="label" ID="lbltxtUserId" runat="server" Text="User-ID:"></asp:Label>
                <asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValUserID" runat="server" ControlToValidate="txtUserId" CssClass="ErrorMessage" ErrorMessage="Required.">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="reqExpValUserId" runat="server" ControlToValidate="txtUserId" CssClass="ErrorMessage" ErrorMessage="Invalid." ValidationExpression="[x|X]?[a-zA-Z]{2}[1-9]{1}[0-9]{2,4}"></asp:RegularExpressionValidator>
            </p>
            <p>
                <label class="label" for="txtUsrPwd" title="Need to be complex.">Password:</label>
                <asp:TextBox ID="txtUsrPwd" runat="server" CssClass="pwd"></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValPwd" runat="server" ControlToValidate="txtUsrPwd" CssClass="ErrorMessage" ErrorMessage="Required.">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="regexpUsrPwd" runat="server" ControlToValidate="txtUsrPwd" CssClass="ErrorMessage" ErrorMessage="Invalid." ValidationExpression="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$"></asp:RegularExpressionValidator>
            </p>
            <p>
                <label class="label" for="txtUserFirstName">Firstname:</label>
                <asp:TextBox ID="txtUserFirstName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValFirstName" runat="server" ControlToValidate="txtUserFirstName" CssClass="ErrorMessage" ErrorMessage="Required."><span title="Firstname is required.">*</span></asp:RequiredFieldValidator>
            </p>
            <p>
                <label class="label" for="txtUserLastName">Lastname:</label>
                <asp:TextBox ID="txtUserLastName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValLastName" runat="server" ControlToValidate="txtUserLastName" CssClass="ErrorMessage" ErrorMessage="Required."><span title="Familyname is required.">*</span></asp:RequiredFieldValidator>
            </p>
            <p>
                <asp:Label CssClass="label" ID="lbltxtUserFullName" runat="server" Text="Fullname:"></asp:Label>                    
                <asp:TextBox ID="txtUserFullName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValFullName" runat="server" ControlToValidate="txtUserFullName" CssClass="ErrorMessage" ErrorMessage="Required."><span title="Fulname is required.">*</span></asp:RequiredFieldValidator>
                <asp:CustomValidator ClientValidationFunction="validateFullName" ControlToValidate="txtUserFullName" CssClass="ErrorMessage" OnServerValidate="reqCustFullName_ServerValidate" runat="server" ErrorMessage="Invalid"  ></asp:CustomValidator>
            </p>
            <p>
                <asp:Label CssClass="label" ID="lbltxtqrycompanykey" title="Suggestions after 3 characters ..." runat="server" Text="Company Key:"></asp:Label>                                        
                <asp:TextBox ID="txtqrycompanykey" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="reqValcompanyKey" runat="server" CssClass="ErrorMessage" ErrorMessage="Required." ControlToValidate="txtqrycompanykey"><span title="Company key is required.">*</span></asp:RequiredFieldValidator>                
            </p>
            <p>
                <asp:Label CssClass="label" ID="lbltxtUsrPersonnelno" runat="server" Text="Personnel Number:"></asp:Label>                                        
                <asp:TextBox ID="txtUsrPersonnelno" runat="server"></asp:TextBox>
                <asp:RegularExpressionValidator ID="reqExpValPersonnelNo" runat="server" ControlToValidate="txtUsrPersonnelno" CssClass="ErrorMessage" ErrorMessage="Invalid." ValidationExpression="[0]{2}[0-9]{6}"></asp:RegularExpressionValidator>
            </p>
            <p>
                <asp:Label CssClass="label" ID="lbldrpdlstOUs" runat="server" Text="Organizational Unit:"></asp:Label>                      
                <asp:DropDownList ID="drpdlstOUs" runat="server" AutoPostBack="True" CssClass="selects" OnTextChanged="drpdlstOUs_TextChanged"></asp:DropDownList>
                <asp:CheckBox ID="chkFilterOU" runat="server" AutoPostBack="True" Checked="True" OnCheckedChanged="chkFilterOU_CheckedChanged" Text="Show only 'Users' OUs" />
                <asp:CustomValidator ID="reqValCustOU" runat="server" ClientValidationFunction="validateOUs" ControlToValidate="drpdlstOUs" CssClass="ErrorMessage" ErrorMessage="Required." OnServerValidate="reqValCustOU_ServerValidate"><span title="OU need to be selected.">*</span></asp:CustomValidator>
            </p>
            <p>
                <asp:HiddenField ID="htxttimezone" runat="server" />
            </p>
            <p></p>
            <h1>Additional information for new user:</h1>
            <p></p>
            <h2>Choose mail-option:</h2>
            <p>
                <asp:RadioButtonList ID="rdbtnlstMailChoice" runat="server" CssClass="radios" RepeatDirection="Horizontal" CellSpacing="5" AutoPostBack="True" OnSelectedIndexChanged="rdbtnlstMailChoice_SelectedIndexChanged">
                    <asp:ListItem Value="0">No Mail information</asp:ListItem>
                    <asp:ListItem Value="1">Add extenal Mailaddress</asp:ListItem>
                    <asp:ListItem Value="2" Selected="True">Create Mailbox</asp:ListItem>
                </asp:RadioButtonList>                     
            </p>
            <p>                   
                <asp:Label CssClass="label" ID="lbldrpdlstExDBs" runat="server" Text="Mailbox Database:"></asp:Label>                    
                <asp:DropDownList ID="drpdlstExDBs" runat="server" CssClass="selects-min" OnTextChanged="drpdlstExDBs_TextChanged"></asp:DropDownList>
                <asp:CustomValidator ID="reqValCustEXDB" runat="server" ControlToValidate="drpdlstExDBs" ClientValidationFunction="validateExDBs" OnServerValidate="reqValCustEXDB_ServerValidate" CssClass="ErrorMessage" ErrorMessage="Invalid."><span title="Exchange Database need to be selected.">*</span></asp:CustomValidator>

                <asp:Label CssClass="label" ID="lbldrpdlstExMbOwaPols" runat="server" Text="Owa Policy:"></asp:Label>                    
                <asp:DropDownList ID="drpdlstExMbOwaPols" runat="server" CssClass="selects-min" OnTextChanged="drpdlstExMbOwaPols_TextChanged"></asp:DropDownList>                    
            </p>
            <p>
                <asp:Label CssClass="label" ID="lbldrpdlstExMbRetPols" runat="server" Text="Retention Policy:"></asp:Label>                    
                <asp:DropDownList ID="drpdlstExMbRetPols" runat="server" CssClass="selects-min" OnTextChanged="drpdlstExMbRetPols_TextChanged"></asp:DropDownList>
            </p>
            <p>
                <asp:Label CssClass="label" ID="lbltxtUserMail" runat="server" Text="External E-Mailaddress:"></asp:Label>                    
                <asp:TextBox ID="txtUserMail" runat="server"></asp:TextBox>
                <asp:RegularExpressionValidator ID="reqExpValMailaddr" runat="server" ControlToValidate="txtUserMail" CssClass="ErrorMessage" ErrorMessage="Invalid." ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                <asp:CustomValidator ID="regexCusValEmail" runat="server" ControlToValidate="txtUserMail" CssClass="ErrorMessage" ErrorMessage="Only external." ClientValidationFunction="validateMailaddr"></asp:CustomValidator>
            </p>    
            <p>
                <asp:Label CssClass="label" ID="lbltxtUsrFaxDir" runat="server" Text="Fax / DIR Entry:"></asp:Label>                    
                <asp:TextBox ID="txtUsrFaxDir" runat="server" CssClass="input-min"></asp:TextBox>
            </p>                
            <p>
                <label class="label" title="Click to select calendar. Click twice to wipe." for="datepicker">Expiration date:</label>
                <asp:TextBox ID="datepicker" runat="server" Width="230px" ReadOnly="True"></asp:TextBox>
                <asp:HiddenField ID="htxtdate" runat="server" ClientIDMode="Static" />   
                <label class="label" for="drpdlstDFSHome">Create HomeDrive:</label>                                                
                <asp:DropDownList ID="drpdlstDFSHome" runat="server" CssClass="selects-min" OnTextChanged="drpdlstDFSHome_TextChanged"></asp:DropDownList>
                <asp:CheckBox ID="chkCrtHomeDrive" runat="server" />    
            </p>                                  
            <p>
                <label class="label" for="txtUserStreet">Street:</label>
                <asp:TextBox ID="txtUserStreet" runat="server" Width="230px"></asp:TextBox>
                <label class="label" for="txtUserCity">City:</label>
                <asp:TextBox ID="txtUserCity" runat="server" Width="230px"></asp:TextBox>
            </p>
            <p>
                <label class="label" for="txtUserZIP">ZIP:</label>
                <asp:TextBox ID="txtUserZIP" runat="server" Width="230px"></asp:TextBox>
                <label class="label" for="txtUserSkype">Skype:</label>
                <asp:TextBox ID="txtUserSkype" runat="server" Width="230px"></asp:TextBox>
            </p>
            <p>
                <label class="label" for="txtUserPhone">Phone:</label>
                <asp:TextBox ID="txtUserPhone" runat="server" Width="230px"></asp:TextBox>                    
            </p>
            <p></p>
            <h2>Maintain group membership:</h2>
            <p></p>
            <p>
                <label class="label" for="lstbxUsrGrps">Groups:</label>
                <asp:ListBox ID="lstUserGroupsQ" runat="server" Rows="10" SelectionMode="Multiple" CssClass="selects" ClientIDMode="Static"></asp:ListBox>
                    
                <span class="labellft" title="Works only if form is correct filled.">
                <asp:LinkButton ID="lbtnRemoveGroup" runat="server" ClientIDMode="Static" EnableViewState="False" Onclick="lbtnRemoveGroup_Click">››&nbsp; Remove selected.</asp:LinkButton>
                </span>
            </p>
            <p>
                <label class="label" for="lstbxUsrGrps" title="Suggestions after 5 characters ...">Search Group:</label>
                <asp:TextBox ID="txtgroupsearch" runat="server"></asp:TextBox>
                    
                <span class="labellft" title="Works only if form is correct filled."><asp:LinkButton ID="lbtnAddGroup" runat="server" ClientIDMode="Static" EnableViewState="False" OnClick="lbtnAddGroup_Click">››&nbsp;Add selected.</asp:LinkButton>
                </span>
            </p>
            <p>                    
                <asp:HiddenField ID="htxtnewgroup" runat="server" ClientIDMode="Static" />                    
            </p>
            <p></p>
            <h2>Last step:</h2>
            <p></p>
            <p>                 
                <span class="labelcnt">
                    <asp:LinkButton ID="lbtnCreateUser" runat="server" ClientIDMode="Static" EnableViewState="False" CausesValidation="True" OnClick="lbtnCreateUser_Click">&rsaquo;&rsaquo;&nbsp;Create User.</asp:LinkButton>
                </span>                               
            </p>           

            <asp:Panel runat="server" id="AppFeedbackUI" visible="false" ClientIDMode="Static" EnableViewState="False">
                <p> 
                    <h2>Result:</h2><br />
                    <asp:Label ID="lblAppFeedback" runat="server"></asp:Label>                                        
                    <span class="labelcnt" title="Click to emtpy the fields and create a new user.">
                        <asp:LinkButton ID="lbtnRestartApp" runat="server" ClientIDMode="Static" EnableViewState="False" CausesValidation="false" OnClick="lbtnRestartApp_Click">&rsaquo;&rsaquo;&nbsp;Restart.</asp:LinkButton>
                    </span>                        
                </p>
            </asp:Panel>
            
        </ContentTemplate>
    </asp:UpdatePanel>

    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanelusr">
        <ProgressTemplate>
            <div class="PleaseWait">
                Working, one moment ...
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>       
    
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cpClientScript" Runat="Server">
    Authenticated as: <asp:Label ID="lblAuthInfo" runat="server"></asp:Label>
 
    <script>
        $(document).ready(function () {

            $('.divExistUsr').hide();

            $('[title]').tooltip();        

            $(".lbtnclLoadExistUsr").bind("click", function (evt) {
                var $divExist = $('.divExistUsr');
                if ($divExist.is(':hidden')) {
                    $divExist.slideDown();
                } else {
                    $divExist.slideUp();
                }                
            });

            $("#<%=datepicker.ClientID %>").datepicker({
                minDate: "+1w",
                maxDate: "+5Y",
                dateFormat: "yy-mm-dd",
                showAnim: "blind",
                altField: "[id$=htxtdate]"
            });

            if ($("[id$=htxtdate]").attr("Value") != null) {
                $("[id$=datepicker]").datepicker("setDate", new Date($("[id$=htxtdate]").attr("Value")));
            }

            $("#<%=txtqrycompanykey.ClientID %>").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '<%=ResolveUrl("~/WSautocomplete.asmx/GetComKey") %>',
                        data: "{ 'companykey': '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split(',')[1],
                                    val: item.split(',')[0]
                                }
                            }))
                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },
                select: function (e, i) {
                    var cmpkey = i.item.val + "," + i.item.label;                    
                    $("#<%=htxtCompanyKey.ClientID %>").val(cmpkey);
                },
                  minLength: 3
              });


            
                $("#<%=txtgroupsearch.ClientID %>").autocomplete({
                     source: function (request, response) {
                         $.ajax({
                             url: '<%=ResolveUrl("~/WSautocomplete.asmx/GetADGroup") %>',
                    data: "{ 'groupname': '" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('^')[0],
                                val: item.split('^')[1]
                            }
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
                select: function (e, i) {                    
                    var grpval = i.item.val;
                    var grpname = i.item.label;
                    $("#<%=htxtnewgroup.ClientID %>").val(grpval + '^' + grpname);
                },
                minLength: 5
            });

            
            $("#<%=datepicker.ClientID %>").dblclick(function () {
                $(this).val("");
            });

            $("#<%=lstUserGroupsQ.ClientID %> option:contains(+)").addClass("newgroups");         
                     
        });
        

        function validateOUs(source, args) {
            var usrOU = document.getElementById('<%=drpdlstOUs.ClientID%>');
               if (usrOU.value != '_Please choose:') {
                   args.IsValid = true;
               }
               else {
                   usrOU.focus();
                   args.IsValid = false;
               }
        }

        function validateExDBs(source, args) {
            var exDB = document.getElementById('<%=drpdlstExDBs.ClientID%>');
                if (exDB.value != '_Please choose:') {
                    args.IsValid = true;
                }
                else {
                    exDB.focus();
                    args.IsValid = false;
                }
            }
        
        function validateMailaddr(source, args) {
            var usrMail = document.getElementById('<%=txtUserMail.ClientID%>');
            var usrMailval = usrMail.value;
             // Customize here.
             if ((usrMailval.indexOf('ads.') == -1) && (usrMailval.indexOf('ads-') == -1)) {
                 args.IsValid = true;
             }
             else {
                 usrMail.focus();
                 args.IsValid = false;
             }
         }

        function validateFullName(source, args) {
            var fulln = document.getElementById('<%=txtUserFullName.ClientID%>');            
            var fullnval = fulln.value;            
            if (fullnval.length > 5) {
                args.IsValid = true;
            }
            else {
                fulln.focus();
                args.IsValid = false;
            }
          }



        // Register script to run at page load:

        Sys.Application.add_load(GetSetTimeZone);

        function GetSetTimeZone() {
            var tztag = document.getElementById('<%=htxttimezone.ClientID%>');
            var timezone = jstz.determine();
            tztag.value = timezone.name();          
        }



        //re-create to make it after alive after postbacks

        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function () {            
        
            $('[title]').tooltip();           

            $(".lbtnclLoadExistUsr").bind("click", function (evt) {
                var $divExist = $('.divExistUsr');
                if ($divExist.is(':hidden')) {                    
                    $divExist.slideDown();
                } else {
                    $divExist.slideUp();
                }                
            });

            $("#<%=datepicker.ClientID %>").datepicker({
                minDate: "+1w",
                maxDate: "+5Y",
                dateFormat: "yy-mm-dd",
                showAnim: "blind",
                altField: "[id$=htxtdate]"
            });

            if ($("[id$=htxtdate]").attr("Value") != null) {
                $("[id$=datepicker]").datepicker("setDate", new Date($("[id$=htxtdate]").attr("Value")));
            }

            $("#<%=datepicker.ClientID %>").dblclick(function () {
                $(this).val("");
            })

            $("#<%=txtqrycompanykey.ClientID %>").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '<%=ResolveUrl("~/WSautocomplete.asmx/GetComKey") %>',
                        data: "{ 'companykey': '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.split(',')[1],
                                    val: item.split(',')[0]
                                }
                            }))
                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },
                  select: function (e, i) {
                      var cmpkey = i.item.val + "," + i.item.label;
                      $("#<%=htxtCompanyKey.ClientID %>").val(cmpkey);
                },
                  minLength: 3
              });
            
           
                $("#<%=txtgroupsearch.ClientID %>").autocomplete({
                     source: function (request, response) {
                         $.ajax({
                             url: '<%=ResolveUrl("~/WSautocomplete.asmx/GetADGroup") %>',
                             data: "{ 'groupname': '" + request.term + "'}",
                             dataType: "json",
                             type: "POST",
                             contentType: "application/json; charset=utf-8",
                             success: function (data) {
                                 response($.map(data.d, function (item) {
                                     return {
                                         label: item.split('^')[0],
                                         val: item.split('^')[1]
                                     }
                                 }))
                             },
                             error: function (response) {
                                 alert(response.responseText);
                             },
                             failure: function (response) {
                                 alert(response.responseText);
                             }
                         });
                     },
                    select: function (e, i) {                    
                        var grpval = i.item.val;
                        var grpname = i.item.label;
                        $("#<%=htxtnewgroup.ClientID %>").val(grpval + '^' + grpname);
                },
                    minLength: 5
                });

            $("#<%=lstUserGroupsQ.ClientID %> option:contains(+)").addClass("newgroups");
            

        });


    </script>   
    
</asp:Content>

